package com.ruoyi.system.es;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.common.collect.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * es配置
 */
@Data
@Slf4j
@Component
@Configuration
@ConfigurationProperties(prefix = "es")
public class EsConfig {


    private String ip;
    private Integer port;

    private int MAX_CONN_TOTAL = 200;
    private int MAX_CONN_PER_ROUTE = 20;

//    @Bean
//    public ElasticsearchClient elasticsearchClient(){
//        RestClient httpClient = RestClient.builder(new HttpHost(ip,port))
//                .setHttpClientConfigCallback(httpClientBuilder
//                        ->httpClientBuilder.setDefaultHeaders(
//                        List.of(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString())))
//                        .addInterceptorLast((HttpResponseInterceptor) (response, context)
//                                -> response.addHeader("X-Elastic-Product", "Elasticsearch")))
//                .build();
//        ElasticsearchTransport transport = new RestClientTransport(httpClient, new JacksonJsonpMapper());
//        ElasticsearchClient client = new ElasticsearchClient(transport);
//        log.info("初始化es客户端成功！");
//        return client;
//    }

//    @Bean
//    public ElasticsearchClient elasticsearchClient(){
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials("elastic", "123456"));
//
//        HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create()
//                .setDefaultCredentialsProvider(credentialsProvider)
//                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
//                .setMaxConnTotal(this.MAX_CONN_TOTAL)
//                .setMaxConnPerRoute(this.MAX_CONN_PER_ROUTE);
//
//        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(ip, port))
//                .setHttpClientConfigCallback(httpClientBuilder1 -> httpClientBuilder);
//
//        restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//            @Override
//            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
//                return httpClientBuilder.setDefaultHeaders(Collections.singletonMap("X-Elastic-Product", "elasticsearch-js"));
//            }
//        });
//        RestClient restClient = restClientBuilder.build();
//
//
//        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//        ElasticsearchClient client = new ElasticsearchClient(transport);
//        log.info("初始化es客户端成功！");
//        return client;
//    }

    @Bean
    public ElasticsearchClient elasticsearchClient(){
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("elastic", "123456"));

        HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create()
                .setDefaultCredentialsProvider(credentialsProvider)
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                .setMaxConnTotal(this.MAX_CONN_TOTAL)
                .setMaxConnPerRoute(this.MAX_CONN_PER_ROUTE);
        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(ip, port,"http"))
                .setHttpClientConfigCallback(httpClientBuilder1 -> httpClientBuilder
                .setDefaultCredentialsProvider(credentialsProvider)
                .addInterceptorLast((HttpResponseInterceptor)
                        (response, context) ->
                                response.addHeader("X-Elastic-Product", "Elasticsearch")));

        RestClient restClient = restClientBuilder.build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }
}
